#!/bin/bash
#
# $Id$
#

usage() {
echo `basename $0` [host] ...
}

[ -z "$USER" ] && USER=admin
LOGFILE=`basename $0 .sh`.log

[ -f $USER-keys.tar ] && tar -xf $USER-keys.tar
if [ ! -d $USER-keys ]; then
    echo "not find directory \"$USER-keys\". exit"
    exit
fi

### build srvlist
srvlist=$*
[ -z "$srvlist" -a -f srvlist.lst ] && srvlist=`cat srvlist.lst`
[ -z "$srvlist" ] && echo "unknown hosts"

this_ip=$(echo $(/sbin/ifconfig | grep "inet addr:" | awk '{print $2}' | awk -F: '{print $2}'))
this_name=$(hostname | awk -F. '{print $1}')

### put keys into hosts
for server in $srvlist
do
    echo "-- $server -- " | tee -a $LOGFILE
        servname=$(echo $server | awk -F. '{print $1}')
        echo " $this_name localhost " | grep " $servname " >/dev/null && { echo "cancel, this is monitoring server"; continue; } 
        echo " $this_ip " | grep " $server " >/dev/null && { echo "cancel, this is monitoring server"; continue; }
    scp -o BatchMode=no -p -r $USER-keys/.ssh* $USER@$server:/home/$USER/ >> $LOGFILE 2>&1 && echo Ok || echo Err
done

exit 0

